package atmGUI.service.Impl;

import atmGUI.dao.BaseDAO;
import atmGUI.dao.UserDAO;
import atmGUI.dao.impl.UserDAOImpl;
import atmGUI.pojo.UserDO;
import atmGUI.service.UserService;


public class UserServiceImpl extends BaseDAO implements UserService {
    private final UserDAO userDAO = new UserDAOImpl();
    @Override
    public UserDO getByUserAccountAndPassword(String userAccount, String password) {
        return userDAO.getByUserAccountAndPassword(userAccount, password);
    }

    @Override
    public void saveMoney(int id, double balance) {
        UserDO userDO = userDAO.getById(id);
        userDO.setBalance(userDO.getBalance() + balance);
        userDAO.update(userDO);
    }

    @Override
    public void takeMoney(int id, double balance) throws Exception {
        UserDO userDO = userDAO.getById(id);
        if (balance > userDO.getBalance()) {
            throw new Exception("余额不足");
        }
        userDO.setBalance(userDO.getBalance() - balance);
        userDAO.update(userDO);
    }

    @Override
    public void changePassword(int id, String iptOldPwd, String iptNewPwd, String iptConfirmPwd) throws Exception {
        UserDO userDO = userDAO.getById(id);
        String loginOldPwd = userDO.getPassword();//原有的密码
        boolean bl = iptNewPwd.matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$");
        if (loginOldPwd.equals(iptOldPwd)) {
            if (!bl) {
                throw new Exception("密码格式错误");
            } else if (iptOldPwd.equals(iptNewPwd)) {
                throw new Exception("新密码不能和旧密码相同,请重新输入");
            } else if (!iptNewPwd.equals(iptConfirmPwd)) {
                throw new Exception("新密码和确认密码不一致,请重新输入");
            } else {
                userDO.setPassword(iptConfirmPwd);
                userDAO.update(userDO);
            }
        } else {
            throw new Exception("旧密码不匹配,请重新输入");
        }
    }

    @Override
    public void transfer(int id, String otherAccount, double transferMoney) throws Exception {
        UserDO loginUserDO = userDAO.getById(id);
        UserDO otherUserDO = userDAO.getByUserAccount(otherAccount);
        // 判断账号是否存在
        if (otherUserDO == null) {
            throw new Exception("查无此人");
        }
        // 判断是否转给自己
        if (loginUserDO.getAccount().equals(otherAccount)) {
            throw new Exception("不能转给自己");
        }
        // 判断余额是否足够
        if (transferMoney > loginUserDO.getBalance()) {
            throw new Exception("余额不足");
        }
        // 当前账户扣钱
       // this.takeMoney(id,transferMoney);
        loginUserDO.setBalance(loginUserDO.getBalance() - transferMoney);
        //对方账户加钱
      //  this.saveMoney(otherUserDO.getId(),transferMoney);
        otherUserDO.setBalance(otherUserDO.getBalance() + transferMoney);
        try {
            getConnection();
            setAutoCommit(false);
            userDAO.update(loginUserDO);
            userDAO.update(otherUserDO);
            commit();
        } catch (Exception e1) {
            rollback();
            throw new Exception("转账失败");
        } finally {
            close();
        }
    }

}
